<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class Paymentlog_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_payment_log';
	}
        
         public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => "DESC",
						'searchoptions' => FALSE
				),
				array(
						'name' => 'customersName',
						'label' => $this->lang->line('customersName'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'booking_place_code',
						'label' => $this->lang->line('booking_place_code'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
				array(
						'name' 	=> 'surplus',
						'label' => $this->lang->line('surplus'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
                                array(
						'name' 	=> 'thucnhan',
						'label' => $this->lang->line('thucnhan'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
                                array(
						'name' 	=> 'thucluu',
						'label' => $this->lang->line('thucluu'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'residual',
						'label' => $this->lang->line('residual'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
                                array(
						'name' 	=> 'note',
						'label' => $this->lang->line('note'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'updatedby',
						'label' => $this->lang->line('updatedby'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				)
		);
	}
	
	public function json_data($controller, $right){
		$this->datatables
		->select("tbl.id,tbl.booking_id,tbl.surplus,tbl.thucnhan,tbl.amount,tbl.residual,tbl.note,tbl.lastupdated,bc.name,usr.username,blb.booking_place_code")
		->from($this->table_name.' AS tbl')
                ->join('book_list_booking AS blb','blb.id = tbl.booking_id','LEFT')
                ->join('book_contacts AS bc','bc.customerID = tbl.customerID')
                ->join('users AS usr','usr.id = tbl.owner');
	
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();
                
		foreach($datatables['aaData'] as $item){
                        //booking_place_code;
                        if($item['booking_place_code'] == NULL){
                            $info = $this->paymentlog_model->getInfoTable(array('booking_id'=>$item['booking_id']),'book_history');
                        }
                        
			$ouput['aaData'][] = array(
                            $item['id'],
                            $item['name'],
                            (isset($item['booking_place_code']))?$item['booking_place_code']:$info[0]->booking_place_code,
                            $this->bookinglib->my_number_format($item['surplus'],2, ',', ','),
                            $this->bookinglib->my_number_format($item['thucnhan'],2, ',', ','),
                            $this->bookinglib->my_number_format($item['amount'],2, ',', ','),
                            $this->bookinglib->my_number_format($item['residual'],2, ',', ','),
                            $item['note'],
                            $item['username'],
                            date("d-m-Y H:i:s",$item['lastupdated'])
			);
		}
	
		return json_encode($ouput);
	}
        
}